<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('管理')"/>
</head>
<body class="hold-transition skin-blue sidebar-mini layout-boxed">
<div class="wrapper">
    <th:block th:include="include :: headbar"/>
    <th:block th:include="include :: sidebar"/>
    <div class="content-wrapper">
        <section class="content-header">
            <h1>&nbsp;</h1>
            <ol class="breadcrumb">
                <li><a href="/index"><i class="fa fa-dashboard"></i> 首页</a></li>
                <li><a href="#">车模型订单管理</a></li>
                <li class="active"> 车模型支付列表</li>
            </ol>
        </section>
        <section class="content">
            <div class="box box-solid">
                <form class="box-header with-border form-inline" id="form">
                    <input type="text" class="form-control input-sm datetimepicker" placeholder="结账时间(起)"
                           name="startTime">
                    <input type="text" class="form-control input-sm datetimepicker" placeholder="结账时间(止)"
                           name="endTime">
                    <button type="button" class="btn btn-success btn-sm btn-flat" onclick="$.table.search('form')"><i
                            class="fa fa-search"></i> 搜索
                    </button>
                    <button type="button" class="btn btn-primary btn-sm btn-flat" onclick="download()"><i
                            class="glyphicon glyphicon-download"></i> 导出
                    </button>
                </form>
                <div class="box-body">
                    <table id="bootstrap-table" data-mobile-responsive="true"></table>
                </div>
                <div class="box-footer" style="text-align: right">
                    总数量：<span id="sum"></span>
                    总价格：<span id="total"></span>
                </div>
            </div>
        </section>
    </div>
    <th:block th:include="include :: copyright"/>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var prefix = "/mall/pay";
    $(function () {
        var options = {
            url: prefix + "/list",
            sortName: "",
            uniqueId: 'payId',
            modalName: "",
            sidePagination: 'client',
            responseHandler: responseHandler,
            columns: [
                {radio: true},
                {
                    title: "序号", formatter: function (value, row, index) {
                        return $.table.serialNumber(index);
                    }
                },
                {field: 'username', title: '用户名'},
                {field: 'deviceId', title: '设备编号'},
                {field: 'size', title: '数量'},
                {field: 'price', title: '价格'},
                {field: 'createTime', title: '付款时间'}
            ]

        };
        $.table.init(options);
    });

    function numAdd(num1, num2) {
        var baseNum, baseNum1, baseNum2;
        try {
            baseNum1 = num1.toString().split(".")[1].length;
        } catch (e) {
            baseNum1 = 0;
        }
        try {
            baseNum2 = num2.toString().split(".")[1].length;
        } catch (e) {
            baseNum2 = 0;
        }
        baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
        return (num1 * baseNum + num2 * baseNum) / baseNum;
    }

    function responseHandler(resp) {
        var sum = 0;
        var total = 0;
        var list = resp.data;
        for (var i = 0; i < list.length; i++) {
            var item = list[i];
            sum += item.size;
            total = numAdd(total, item.price);
        }
        $('#sum').html(sum - 1);
        $('#total').html(total);
        return resp.data;
    }

    function download() {
        var oReq = new XMLHttpRequest();
        oReq.open("GET", prefix + "/export/order", true);
        oReq.responseType = "blob";
        var date = new Date();
        var month = date.getMonth() + 1;
        var now = date.getFullYear() + "-" + month + "-" + date.getDate();
        oReq.onload = function (oEvent) {
            var content = oReq.response;

            var elink = document.createElement('a');
            elink.download = "车模型支付报表" + now
                + ".xlsx";
            elink.style.display = 'none';

            var blob = new Blob([content], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});
            elink.href = URL.createObjectURL(blob);

            document.body.appendChild(elink);
            elink.click();

            document.body.removeChild(elink);
            window.URL.revokeObjectURL(elink.href);
        };
        oReq.send();
    }
</script>
</body>
</html>
